//
//  BVLeftMenuController.m
//  BuyVegetables1
//
//  Created by 叶思超 on 16/1/30.
//  Copyright © 2016年 叶思超. All rights reserved.
//

#import "BVLeftMenuController.h"
#import "UIImage+Extension.h"
#import "BVConst.h"
#import "UIView+Extension.h"
#import "HttpTool.h"
#import "BVMyOrderViewController.h"
#import "BVRecevicedAddressViewController.h"
#import "BVMyDiscountViewController.h"
#import "BVMyInvitedViewController.h"
#import "BVSettingViewController.h"
#import "BVLoginViewController.h"
#import "BVPersonInfomatonViewController.h"
#import "BVMyCollectionViewController.h"
#import "BVUserInstructionsViewController.h"
#import "BVAboutUsViewController.h"

#define buttonW 280
#define buttonH 50
@interface BVLeftMenuController ()
/**
 * 选中按钮
 */
@property (nonatomic, strong) UIButton *selectedButton;
/** 底部view */
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIImageView *imageView;
/**
 *  蒙板
 */
@property (nonatomic, strong) UIButton *coverButton;
/**
 *  拨打电话
 */
@property (nonatomic, strong) UIWebView *phoneCallWebView;

@property (nonatomic,weak) UILabel * nameLabel;
@property (nonatomic,weak) UILabel * telLabel;
@end

@implementation BVLeftMenuController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置UI
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginAndLogout) name:LoginSuccessNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginAndLogout) name:LogOutSuccessNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginAndLogout) name:SaveUserInfoSuccess object:nil];
}
#pragma mark -- 设置UI
- (void)setupUI {
    // 底部view
    self.bottomView = [[UIView alloc] init];
    self.bottomView.frame = CGRectMake(0, 0, BVScreenW, BVScreenH);
    self.bottomView.alpha = 1;
    
    /**
     *  高斯模糊
     */
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
    self.imageView.frame = CGRectMake(0, 0, BVScreenW, BVScreenH);
    self.imageView.userInteractionEnabled = YES;

//    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
//    
//    effectview.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
//    
//    [self.imageView addSubview:effectview];
    [self.imageView addSubview:self.bottomView];
    [self.view addSubview:self.imageView];
    
    // 设置数据
    [self setupData];
    
    // 设置Fram值
    [self setupFrame];
}

#pragma mark -- 设置数据
- (void)setupData {
    // 个人信息
    [self setupButtonWithIcon:@"" title:nil bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"sy_order" title:@" 我的订单" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"my_collection" title:@" 我的收藏" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"sy_address" title:@" 收货地址" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"sy_coupons" title:@"我的优惠券" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"sy_activity" title:@"邀请活动" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"sy_customer_service" title:@"联系客服" bgColor:BVColor(34, 34, 34)];
    [self setupButtonWithIcon:@"aboutus" title:@"关于我们" bgColor:BVColor(34,34,34)];
    [self setupButtonWithIcon:@"usermusttoknow" title:@"用户须知" bgColor:BVColor(34,34,34)];
    [self setupButtonWithIcon:@"sy_set_up" title:@"设置" bgColor:BVColor(34, 34, 34)];
}

/**
 *  添加按钮
 *
 *  @param icon  图片
 *  @param title 标题
 *  @param color button背景颜色
 */
- (UIButton *)setupButtonWithIcon:(NSString *)icon title:(NSString *)title bgColor:(UIColor *)color {
    // 1.创建button
    UIButton *button = [[UIButton alloc] init];
    button.tag = self.bottomView.subviews.count;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:button];
    
    // 2.设置图片和标题
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:17];
    
    // 3.设置按钮选中的背景色
    [button setBackgroundImage:[UIImage imageWithColor:color] forState:UIControlStateSelected];
    
    // 4.按钮的基本设置
    // 4.1 设置高亮选中时不让图标变色
    button.adjustsImageWhenHighlighted = NO;
    
    // 4.2 设置按钮内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    // 4.3 调整按钮间距
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    
    return button;
}

- (void)setupFrame {
    NSInteger buttonCount = self.bottomView.subviews.count;
    
    // 设置按钮的frame
    for (NSInteger i = 0; i < buttonCount; i++) {
        UIButton *button = self.bottomView.subviews[i];
        if (0 == i) {
            [self setUpUserInfoUI];
            
        } else {
            button.x = 0;
            button.y =  40 + 80 + (i - 1) * buttonH;
            button.width = buttonW;
            button.height = buttonH;
            
//            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, buttonH * i + buttonH + 12, buttonW - 40, 0.5)];
//            label.backgroundColor = BVBackgroundLightGrayColor;
//            [self.view addSubview:label];
        }
    }
}

#pragma mark -- 私有方法
/**
 *  button点击事件
 */
- (void)buttonClick:(UIButton *)button {    
//    self.selectedButton.selected = NO;
//    button.selected = YES;
//    self.selectedButton = button;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defaults objectForKey:@"USERINFO"];
    switch (button.tag) {
        case 0: {
            if (dict[@"id"]) {
                BVPersonInfomatonViewController * infoVC = [BVPersonInfomatonViewController new];
                ROOTNAVPUSH(infoVC, YES);
            }
            else{
                [self goLoginVC];
            }
            break;
        }
        case 1: {
            if(dict[@"id"]){
                
                BVMyOrderViewController * myOrderVC = [BVMyOrderViewController new];
                ROOTNAVPUSH(myOrderVC, YES);

            }else{
                [self goLoginVC];
            }
            break;
        }
        case 2: {
            if (dict[@"id"]) {
                BVMyCollectionViewController *myCollection = [BVMyCollectionViewController new];
                ROOTNAVPUSH(myCollection, YES);
            }
            else{
                [self goLoginVC];
            }
            break;
        }
        case 3:{
            if (dict[@"id"]) {
                BVRecevicedAddressViewController *recevicedAddressVC = [[BVRecevicedAddressViewController alloc] init];
                ROOTNAVPUSH(recevicedAddressVC, YES);
            }
            else{
                [self goLoginVC];
            }
            break;
        }
        case 4:{
            if (dict[@"id"]) {
                BVMyDiscountViewController *discountVC = [[BVMyDiscountViewController alloc] init];
                ROOTNAVPUSH(discountVC, YES);
            }
            else{
                [self goLoginVC];
            }
            break;
        }
        case 5:{
            if (dict[@"id"]) {
                BVMyInvitedViewController * inviteVC = [BVMyInvitedViewController new];
                ROOTNAVPUSH(inviteVC, YES);
            }else{
                [self goLoginVC];
            }
            break;
        }
        case 6: {
            // 联系客服
            [self phoneClick];
            break;
        }
        case 7:{
            BVAboutUsViewController * settingVC = [BVAboutUsViewController new];
            ROOTNAVPUSH(settingVC, YES);
            break;
        }
        case 8:{
            BVUserInstructionsViewController * userVC = [[BVUserInstructionsViewController alloc]init];
            ROOTNAVPUSH(userVC, YES);
            break;
        }
        case 9:{
            if (dict[@"id"]) {
                BVSettingViewController *aboutUsVC = [BVSettingViewController new];
                ROOTNAVPUSH(aboutUsVC, YES);
            } else {
                [self goLoginVC];
            }
            break;
        }
        default:
            break;
    }
}

#pragma mark -- 客服
- (void)phoneClick {
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"0531-58192080"]];
    if ( !_phoneCallWebView ) {
        _phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    }
    [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}

#pragma mark -- 取消打电话
- (void)cancelButtonClick {
    [self.coverButton removeFromSuperview];
}

#pragma mark  不登录进入登录界面
- (void)goLoginVC{
    
    BVLoginViewController *loginVC = [[BVLoginViewController alloc] init];

    ROOTNAVPUSH(loginVC, YES);
}
#pragma mark   通知
- (void)loginAndLogout{
    
    [self removeUserInfoUI];
    [self setUpUserInfoUI];
}
- (void)removeUserInfoUI{
    UIButton * btn = self.bottomView.subviews.firstObject;
    for (UIView * view in btn.subviews) {
        [view removeFromSuperview];
    }
}
- (void)setUpUserInfoUI{
    
    UIButton * button = self.bottomView.subviews.firstObject;
    button.x = 0;
    button.y = 40 ;
    button.width = buttonW;
    button.height = 80;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    button.contentEdgeInsets = UIEdgeInsetsMake(-25, 0, 0, 23);
    
    // 判断用户是否已登录
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defaults objectForKey:@"USERINFO"];
    NSString * userid = dict[@"id"];
    NSString * nickname = dict[@"nickname"];
    NSString * phone = dict[@"mobile"];
    

    if (userid != nil) { // 用户已登录
        if (nickname.length == 0) {
            UILabel * nameLabel = [[UILabel alloc] init];
            nameLabel.frame = CGRectMake(30, 10, buttonW - 30, 30);
            nameLabel.text = @"请完善信息";
            nameLabel.textColor = [UIColor whiteColor];
            nameLabel.font = [UIFont systemFontOfSize:20];
            [button addSubview:nameLabel];
            
        } else {
            
            UILabel * nameLabel = [[UILabel alloc] init];
            nameLabel.frame = CGRectMake(30, 0, buttonW - 30, 30);
            nameLabel.text = nickname;
            nameLabel.textColor = [UIColor whiteColor];
            [button addSubview:nameLabel];
        }
        
        UILabel * telLabel = [[UILabel alloc] init];
        telLabel.frame = CGRectMake(30, 35, buttonW, 25);
        telLabel.textColor = [UIColor whiteColor];
        NSMutableString * mutablePhone = [phone mutableCopy];
        [mutablePhone replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        telLabel.text = mutablePhone;
        [button addSubview:telLabel];

    } else {
        // 用户未登录
        UILabel * nameLabel = [[UILabel alloc] init];
        nameLabel.frame = CGRectMake(30, 10, buttonW - 30, 30);
        nameLabel.text = @"登录";
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.font = [UIFont systemFontOfSize:24];
        [button addSubview:nameLabel];
    }
    
    UIView *separatorLine = [[UIView alloc] init];
    separatorLine.frame = CGRectMake(10, button.height - 8, buttonW - 2 * 10, 1);
    separatorLine.backgroundColor = [UIColor whiteColor];
    [button addSubview:separatorLine];

}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
